package cn.mymanagesys.servlet;

import cn.mymanagesys.entity.SysMenu;
import cn.mymanagesys.service.SysMenuService;
import cn.mymanagesys.service.impl.SysMenuServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@WebServlet("/menu")
public class MenuServlet extends HttpServlet {

    private SysMenuService sysMenuService = new SysMenuServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        // 用来装  title、logoInfo、menuInfo 的数据
        HashMap<String, Object> map = new HashMap<>();


        /**首页开始**/

        HashMap<String, String> homeInfoMap_chid = new HashMap<>();
        homeInfoMap_chid.put("title","首页");
        homeInfoMap_chid.put("href","welcome.jsp");

        map.put("homeInfo",homeInfoMap_chid);
        /**首页结束**/

        /**logoInfo开始**/

        HashMap<String, String> logoInfoMap_chid = new HashMap<>();
        logoInfoMap_chid.put("title","LAYUI MINI");
        logoInfoMap_chid.put("image","statics/images/logo.png");

        map.put("logoInfo",logoInfoMap_chid);
        /**logoInfo结束**/



        /**menuInfo开始**/
        List<SysMenu> menuList = sysMenuService.menusTree();

        // 把菜单放到list里
        ArrayList<Object> parentMenuList = new ArrayList<>();

        // 循环 父级菜单
        for (SysMenu sysMenu : menuList) {
            // 用map封装单个菜单信息
            HashMap<String, Object> menuInfoMap_parent = new HashMap<>();
            menuInfoMap_parent.put("title",sysMenu.getMenuName());
            menuInfoMap_parent.put("icon",sysMenu.getIcon());
            menuInfoMap_parent.put("href",sysMenu.getUrl());
            menuInfoMap_parent.put("target",sysMenu.getTarget());

            ArrayList<Object> childrenMenuList = new ArrayList<>();
            // 循环 子级菜单
            for (SysMenu child : sysMenu.getChildren()) {
                HashMap<String, Object> menuInfoMap_child = new HashMap<>();
                menuInfoMap_child.put("title",child.getMenuName());
                menuInfoMap_child.put("icon",child.getIcon());
                menuInfoMap_child.put("href",child.getUrl());
                menuInfoMap_child.put("target",child.getTarget());

                childrenMenuList.add(menuInfoMap_child);
            }

            menuInfoMap_parent.put("child",childrenMenuList);


            parentMenuList.add(menuInfoMap_parent);
        }

        map.put("menuInfo",parentMenuList);


        /**menuInfo结束**/

        String jsonString = JSON.toJSONString(map);

        response.getWriter().print(jsonString);

        response.getWriter().flush();
        response.getWriter().close();
    }
}
